import { forwardRef, useImperativeHandle, useState } from 'react';

import { useRequest } from 'ahooks';
import { Button, DatePicker, Drawer, Form, FormProps, Input, InputNumber, Space } from 'antd';
import dayjs from 'dayjs';

import { store } from '@/store';

import { proDataSewageAdd, proDataSewageUpdate } from '@/services/bizApi/wushuishujujiekou';
import { ProFormSelect } from '@ant-design/pro-form';

import { BizObject } from '.';

type EditOpenBaseArgs = {
  /** 成功回调 */
  onSuccess?: () => void;
};

export type EditRef = {
  open: (
    arg:
      | (EditOpenBaseArgs & {
          type: 'add';
        })
      | (EditOpenBaseArgs & {
          type: 'edit';
          rowData: BizObject;
        })
      | (EditOpenBaseArgs & {
          type: 'detail';
          rowData: BizObject;
        })
  ) => void;
};

export type EditProps = {};

const Edit: React.ForwardRefRenderFunction<EditRef, EditProps> = (_, ref) => {
  const [form] = Form.useForm();
  // 弹窗开启状态
  const [open, setOpen] = useState(false);
  const [args, setArgs] = useState<ArgumentsType<EditRef['open']>[0]>();

  const submit = useRequest(
    async ({ _type, ...values }) => {
      const query = {
        ...values
      };
      if (args?.type === 'edit') {
        // 编辑接口
        await proDataSewageUpdate({
          ...query,

          id: args?.rowData?.id
        });
      } else {
        await proDataSewageAdd(query);
      }

      args?.onSuccess?.();
      setOpen(false);

      store.app?.message.success(args?.type === 'add' ? '新增成功' : '编辑成功');
    },
    {
      manual: true
    }
  );

  useImperativeHandle(ref, () => {
    return {
      open: (arg) => {
        setArgs(arg);
        setOpen(true);

        setTimeout(() => {
          // 编辑回填数据
          if ((arg?.type === 'edit' || arg?.type === 'detail') && arg?.rowData) {
            // 回填数据
            form.setFieldsValue({
              ...arg.rowData,
              rq: dayjs(arg.rowData?.rq)
            });
          } else {
            form.resetFields();
          }
        });
      }
    };
  });

  const onFinish: FormProps['onFinish'] = async (values) => {
    return submit.runAsync(values);
  };

  const onCancel = () => {
    if (submit.loading) {
      store.app?.message.info('正在提交中，请稍后');
      return;
    }
    setOpen(false);
  };

  // 仅保存
  const onSave = () => {
    form.submit();
  };

  return (
    <Drawer
      title={
        {
          add: '新增',
          edit: '编辑',
          detail: '详情'
        }[args?.type as string]
      }
      extra={
        <>
          {args?.type !== 'detail' && (
            <Space>
              <Button
                type="primary"
                loading={submit.loading}
                disabled={submit.loading}
                onClick={onSave}
              >
                确定
              </Button>
              <Button onClick={onCancel} disabled={submit.loading}>
                取消
              </Button>
            </Space>
          )}
        </>
      }
      width="50%"
      open={open}
      onClose={onCancel}
    >
      <Form
        onFinish={onFinish}
        form={form}
        layout="horizontal"
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 14 }}
        disabled={args?.type === 'detail'}
        requiredMark={args?.type !== 'detail'}
        variant={args?.type === 'detail' ? 'borderless' : undefined}
      >
        <Form.Item
          label="日期"
          name="rq"
          rules={[
            {
              required: true,
              message: '请选择日期'
            }
          ]}
        >
          <DatePicker style={{ width: '100%' }} placeholder="请选择日期" />
        </Form.Item>

        <ProFormSelect
          name="xm"
          label={'项目'}
          placeholder="请选择项目"
          rules={[{ required: true, message: '请选择项目' }]}
          request={async () => {
            return (store.dict['productionSite'] || []).map((item) => ({
              value: item.dicValue,
              label: item.dicName
            }));
          }}
        />

        <Form.Item
          label="生产计划"
          name="scjh"
          rules={[
            {
              required: true,
              message: '请输入生产计划'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入生产计划"
          />
        </Form.Item>

        <Form.Item
          label="站内来水"
          name="znls"
          rules={[
            {
              required: true,
              message: '请输入站内来水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入站内来水"
          />
        </Form.Item>

        <Form.Item
          label="厨余来水"
          name="cyls"
          rules={[
            {
              required: true,
              message: '请输入厨余来水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入厨余来水"
          />
        </Form.Item>

        <Form.Item
          label="站外转运来水"
          name="zwzyls"
          rules={[
            {
              required: true,
              message: '请输入站外转运来水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入站外转运来水"
          />
        </Form.Item>

        <Form.Item
          label="格栅进水"
          name="gsjs"
          rules={[
            {
              required: true,
              message: '请输入格栅进水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入格栅进水"
          />
        </Form.Item>

        <Form.Item
          label="生化进水"
          name="shjs"
          rules={[
            {
              required: true,
              message: '请输入生化进水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入生化进水"
          />
        </Form.Item>

        <Form.Item
          label="工艺出水"
          name="gycs"
          rules={[
            {
              required: true,
              message: '请输入工艺出水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入工艺出水"
          />
        </Form.Item>

        <Form.Item
          label="预处理出渣"
          name="yclcz"
          rules={[
            {
              required: true,
              message: '请输入预处理出渣'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入预处理出渣"
          />
        </Form.Item>

        <Form.Item
          label="生化排泥"
          name="shpn"
          rules={[
            {
              required: true,
              message: '请输入生化排泥'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入生化排泥"
          />
        </Form.Item>

        <Form.Item
          label="化学排泥"
          name="hxpn"
          rules={[
            {
              required: true,
              message: '请输入化学排泥'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入化学排泥"
          />
        </Form.Item>

        <Form.Item
          label="脱水生化污泥"
          name="dsshwn"
          rules={[
            {
              required: true,
              message: '请输入脱水生化污泥'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入脱水生化污泥"
          />
        </Form.Item>

        <Form.Item
          label="脱水化学污泥"
          name="dshxwn"
          rules={[
            {
              required: true,
              message: '请输入脱水化学污泥'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入脱水化学污泥"
          />
        </Form.Item>

        <Form.Item
          label="自来水用量"
          name="zlsyl"
          rules={[
            {
              required: true,
              message: '请输入自来水用量'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入自来水用量"
          />
        </Form.Item>

        <Form.Item
          label="中水回用量"
          name="zshyl"
          rules={[
            {
              required: true,
              message: '请输入中水回用量'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入中水回用量"
          />
        </Form.Item>

        <Form.Item
          label="电量"
          name="dl"
          rules={[
            {
              required: true,
              message: '请输入电量'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入电量" />
        </Form.Item>

        <Form.Item
          label="一体化来水"
          name="ythls"
          rules={[
            {
              required: true,
              message: '请输入一体化来水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入一体化来水"
          />
        </Form.Item>

        <Form.Item
          label="一体化进水"
          name="ythjs"
          rules={[
            {
              required: true,
              message: '请输入一体化进水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入一体化进水"
          />
        </Form.Item>

        <Form.Item
          label="一体化出水"
          name="ythcs"
          rules={[
            {
              required: true,
              message: '请输入一体化出水'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入一体化出水"
          />
        </Form.Item>

        <Form.Item
          label="一体化排泥"
          name="ythpn"
          rules={[
            {
              required: true,
              message: '请输入一体化排泥'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入一体化排泥"
          />
        </Form.Item>

        <Form.Item
          label="污水厂剩余库容"
          name="wscsykr"
          rules={[
            {
              required: true,
              message: '请输入污水厂剩余库容'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入污水厂剩余库容"
          />
        </Form.Item>

        <Form.Item
          label="应急池剩余库容"
          name="yjcsykr"
          rules={[
            {
              required: true,
              message: '请输入应急池剩余库容'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入应急池剩余库容"
          />
        </Form.Item>

        <Form.Item
          label="生产异常情况说明"
          name="scycqksm"
          rules={[
            {
              required: true,
              message: '请输入生产异常情况说明'
            }
          ]}
        >
          <Input.TextArea placeholder="请输入生产异常情况说明" />
        </Form.Item>

        <Form.Item
          label="进水COD"
          name="jscod"
          rules={[
            {
              required: true,
              message: '请输入进水COD'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入进水COD"
          />
        </Form.Item>

        <Form.Item
          label="进水氨氮"
          name="jshdan"
          rules={[
            {
              required: true,
              message: '请输入进水氨氮'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入进水氨氮"
          />
        </Form.Item>

        <Form.Item
          label="进水总氮"
          name="jszd"
          rules={[
            {
              required: true,
              message: '请输入进水总氮'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入进水总氮"
          />
        </Form.Item>

        <Form.Item
          label="出水COD"
          name="cscod"
          rules={[
            {
              required: true,
              message: '请输入出水COD'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入出水COD"
          />
        </Form.Item>

        <Form.Item
          label="出水氨氮"
          name="cshdan"
          rules={[
            {
              required: true,
              message: '请输入出水氨氮'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入出水氨氮"
          />
        </Form.Item>

        <Form.Item
          label="出水总氮"
          name="cszd"
          rules={[
            {
              required: true,
              message: '请输入出水总氮'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入出水总氮"
          />
        </Form.Item>

        <Form.Item
          label="出水总磷"
          name="cszp"
          rules={[
            {
              required: true,
              message: '请输入出水总磷'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入出水总磷"
          />
        </Form.Item>

        <Form.Item
          label="植物除臭药剂"
          name="zwccyj"
          rules={[
            {
              required: true,
              message: '请输入植物除臭药剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入植物除臭药剂"
          />
        </Form.Item>

        <Form.Item
          label="次氯酸钠"
          name="clsd"
          rules={[
            {
              required: true,
              message: '请输入次氯酸钠'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入次氯酸钠"
          />
        </Form.Item>

        <Form.Item
          label="复合型碳源液体1"
          name="fhxtyytt1"
          rules={[
            {
              required: true,
              message: '请输入复合型碳源液体1'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入复合型碳源液体1"
          />
        </Form.Item>

        <Form.Item
          label="复合型碳源液体2"
          name="fhxtyytt2"
          rules={[
            {
              required: true,
              message: '请输入复合型碳源液体2'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入复合型碳源液体2"
          />
        </Form.Item>

        <Form.Item
          label="聚氯化铝固体"
          name="jlhglgt"
          rules={[
            {
              required: true,
              message: '请输入聚氯化铝固体'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入聚氯化铝固体"
          />
        </Form.Item>

        <Form.Item
          label="聚氯化铝液体"
          name="jlhhlyt"
          rules={[
            {
              required: true,
              message: '请输入聚氯化铝液体'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入聚氯化铝液体"
          />
        </Form.Item>

        <Form.Item
          label="聚氯化铝饮用水级"
          name="jlhhlyysj"
          rules={[
            {
              required: true,
              message: '请输入聚氯化铝饮用水级'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入聚氯化铝饮用水级"
          />
        </Form.Item>

        <Form.Item
          label="浓硫酸"
          name="nls"
          rules={[
            {
              required: true,
              message: '请输入浓硫酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入浓硫酸" />
        </Form.Item>

        <Form.Item
          label="七水硫酸亚铁"
          name="qslsytt"
          rules={[
            {
              required: true,
              message: '请输入七水硫酸亚铁'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入七水硫酸亚铁"
          />
        </Form.Item>

        <Form.Item
          label="除磷剂"
          name="cpj"
          rules={[
            {
              required: true,
              message: '请输入除磷剂'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入除磷剂" />
        </Form.Item>

        <Form.Item
          label="氯酸钠"
          name="clsnd"
          rules={[
            {
              required: true,
              message: '请输入氯酸钠'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入氯酸钠" />
        </Form.Item>

        <Form.Item
          label="片碱"
          name="pj"
          rules={[
            {
              required: true,
              message: '请输入片碱'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入片碱" />
        </Form.Item>

        <Form.Item
          label="葡萄糖"
          name="ptj"
          rules={[
            {
              required: true,
              message: '请输入葡萄糖'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入葡萄糖" />
        </Form.Item>

        <Form.Item
          label="三氯化铁液体"
          name="sllhtyt"
          rules={[
            {
              required: true,
              message: '请输入三氯化铁液体'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入三氯化铁液体"
          />
        </Form.Item>

        <Form.Item
          label="稀硫酸"
          name="xls"
          rules={[
            {
              required: true,
              message: '请输入稀硫酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入稀硫酸" />
        </Form.Item>

        <Form.Item
          label="盐酸"
          name="ys"
          rules={[
            {
              required: true,
              message: '请输入盐酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入盐酸" />
        </Form.Item>

        <Form.Item
          label="阳离子聚丙烯酰胺1"
          name="ylzblxyam1"
          rules={[
            {
              required: true,
              message: '请输入阳离子聚丙烯酰胺1'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入阳离子聚丙烯酰胺1"
          />
        </Form.Item>

        <Form.Item
          label="阳离子聚丙烯酰胺2"
          name="ylzblxyam2"
          rules={[
            {
              required: true,
              message: '请输入阳离子聚丙烯酰胺2'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入阳离子聚丙烯酰胺2"
          />
        </Form.Item>

        <Form.Item
          label="液碱（32%）"
          name="yj32"
          rules={[
            {
              required: true,
              message: '请输入液碱（32%）'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入液碱（32%）"
          />
        </Form.Item>

        <Form.Item
          label="液碱（50%）"
          name="yj50"
          rules={[
            {
              required: true,
              message: '请输入液碱（50%）'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入液碱（50%）"
          />
        </Form.Item>

        <Form.Item
          label="双氧水"
          name="dyy"
          rules={[
            {
              required: true,
              message: '请输入双氧水'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入双氧水" />
        </Form.Item>

        <Form.Item
          label="消泡剂"
          name="xpj"
          rules={[
            {
              required: true,
              message: '请输入消泡剂'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入消泡剂" />
        </Form.Item>

        <Form.Item
          label="柠檬酸"
          name="mlss"
          rules={[
            {
              required: true,
              message: '请输入柠檬酸'
            }
          ]}
        >
          <InputNumber style={{ width: '100%' }} min={0} precision={3} placeholder="请输入柠檬酸" />
        </Form.Item>

        <Form.Item
          label="阴离子聚丙烯酰胺"
          name="ylzblxyam"
          rules={[
            {
              required: true,
              message: '请输入阴离子聚丙烯酰胺'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入阴离子聚丙烯酰胺"
          />
        </Form.Item>

        <Form.Item
          label="纳滤阻垢剂"
          name="nlfzgj"
          rules={[
            {
              required: true,
              message: '请输入纳滤阻垢剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入纳滤阻垢剂"
          />
        </Form.Item>

        <Form.Item
          label="膜清洗剂酸"
          name="mqxjs"
          rules={[
            {
              required: true,
              message: '请输入膜清洗剂酸'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入膜清洗剂酸"
          />
        </Form.Item>

        <Form.Item
          label="膜清洗剂碱"
          name="mqxjj"
          rules={[
            {
              required: true,
              message: '请输入膜清洗剂碱'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入膜清洗剂碱"
          />
        </Form.Item>

        <Form.Item
          label="洛碛渗滤液非氧化杀菌剂"
          name="lqslfyyxj"
          rules={[
            {
              required: true,
              message: '请输入洛碛渗滤液非氧化杀菌剂'
            }
          ]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={0}
            precision={3}
            placeholder="请输入洛碛渗滤液非氧化杀菌剂"
          />
        </Form.Item>
      </Form>
    </Drawer>
  );
};

export default forwardRef(Edit);
